package com.hongyuan.sellfitness.ui.message.view_mode;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.bus.RxBus;
import com.hongyuan.mvvmhabitx.bus.RxSubscriptions;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.base.ItemViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.data.rxbus.SearchFindMessage;
import com.hongyuan.sellfitness.pushservice.MssagePosition;
import com.hongyuan.sellfitness.ui.message.activity.MsgDetailActivity;
import com.hongyuan.sellfitness.ui.message.beans.MessageLetterBean;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class MessageLetterViewModel extends CustomViewModel<MyRepository> {

    private Disposable mSubscription;

    public MessageLetterViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<MessageLetterItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<MessageLetterItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_letter);

    @Override
    public void onCreate() {
        super.onCreate();

        mSubscription = RxBus.getDefault().toObservable(MssagePosition.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(message -> {
                    observableList.clear();

                    clearParams();
                    requestData(Constants.GET_MSG_CT_LIST);
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        //取消订阅，防止内存泄漏
        RxSubscriptions.remove(mSubscription);
    }

    /**
     * 获取条目下标
     *
     * @param itemViewModel
     * @return
     */
    public int getItemPosition(MessageLetterItemViewModel itemViewModel) {
        return observableList.indexOf(itemViewModel);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_MSG_CT_LIST){
            List<MessageLetterBean> mList = (List<MessageLetterBean>) dataBean;

            observableList.clear();

            for(MessageLetterBean bean : mList){
                MessageLetterItemViewModel itemViewModel = new MessageLetterItemViewModel(this,bean);
                observableList.add(itemViewModel);
            }
        }

        if(code == Constants.UPATE_MSG_READ){
            if(itemViewModel != null){
                itemViewModel.entity.get().setCount(0);
                itemViewModel.entity.notifyChange();

                Bundle bundle = new Bundle();
                bundle.putString("titleName",itemViewModel.entity.get().getTitle());
                bundle.putString("msg_category_id",String.valueOf(itemViewModel.entity.get().getMsg_category_id()));
                startActivity(MsgDetailActivity.class,bundle);

                itemViewModel = null;
            }
        }
    }


    //操作的子项
    private MessageLetterItemViewModel itemViewModel;

    @Override
    public void itemClick(ItemViewModel itemVM) {
        super.itemClick(itemVM);

        itemViewModel = (MessageLetterItemViewModel) itemVM;

    }
}
